package cl.utalca.robot.thread;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cl.utalca.robot.domain.TestCaseDTO;
import cl.utalca.robot.domain.TestCaseResultDTO;
import cl.utalca.robot.domain.TestDTO;

public class InputThread implements Callable<TestCaseResultDTO> {

	String name;
	private static Scanner scanner;
	private TestCaseDTO testCase;
	private TestCaseResultDTO result;
	private Process process;

	Logger logger = LoggerFactory.getLogger(InputThread.class); 
	
	public InputThread(String name, TestCaseDTO testCase, Process process){

		this.name = name;
		this.testCase = testCase;
		this.process = process;
		
	}
	@Override
	public TestCaseResultDTO call() {
		
		logger.info("thread " + name + "running");
		result = runTestCase(testCase, process);
		return result;

	}

	private static TestCaseResultDTO runTestCase(TestCaseDTO testCase, Process process) {

		String result = null;


		PrintStream inputPrinter = new PrintStream(process.getOutputStream());
		inputPrinter.print(testCase.getInput());


		scanner = new Scanner(process.getInputStream());

		inputPrinter.close();
		String salida = "";

		while (scanner.hasNextLine()){

			salida += scanner.nextLine();
			System.out.println(salida);

		}

		int resultado = checkInputOutput(testCase.getOutput(), salida);

		switch (resultado) {
		case 1:

			result = "CODIGO CORRECTO";
			break;

		case 2:

			result = "ERROR DE FORMATO";
			break;
		default:

			result = "CODIGO ERRONEO";
			break;
		}

		scanner = new Scanner(process.getErrorStream());

		String error = "";
		while (scanner.hasNextLine()){

			error += scanner.nextLine();
		}			

		if(!error.isEmpty())
			System.out.println(error);

		process.destroy();

		TestCaseResultDTO testCaseResult = new TestCaseResultDTO();
		testCaseResult.setResult(result);
		testCaseResult.setRunningOutput(salida);
		testCaseResult.setTestCase(testCase);

		return testCaseResult;
	}

	private static String preparePath(File file, TestDTO test ) {

		String[] pathArray = file.getPath().split("\\\\");
		String pathWithoutName = "";

		for(int i = 0; i < pathArray.length-1; i++)
		{
			pathWithoutName += pathArray[i] + "\\";
		}
		pathWithoutName += " " + pathArray[pathArray.length-1].split("\\.")[0];


		//String path = test.getLanguage().getExecuteCommand() + " " + pathWithoutName;
		String path = test.getLanguage().getExecuteCommand().replace("%C", pathWithoutName);

		return path;
	}

	private static int checkInputOutput(String testOutput, String realOutput){

		if(testOutput.equals(realOutput)){

			return 1;
		}
		else if(testOutput.equalsIgnoreCase(realOutput.trim())){

			return 2;
		}
		else 
			return 3;
	}
	public TestCaseResultDTO getResult() {
		return result;
	}
	public void setResult(TestCaseResultDTO result) {
		this.result = result;
	}
	
	
}
